package com.liming;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args) {
        test0();
        test1();
        sum(1,2,3,4,5,6,7,8,9,10);

    }

    private static void test0() {
        TreeSet<Integer> set1 = new TreeSet<>();
        //包装成Integer类型
        set1.add(5);
        set1.add(4);
        set1.add(3);
        set1.add(2);    //自动排序
        set1.add(1);
        System.out.println(set1);
        for (Integer i : set1) {
            System.out.println(i);
        }         //增强for循环
    }

    public static void test1() {
        Comparator<Student> comparator = (o1, o2) -> o1.getAge() - o2.getAge();
        TreeSet<Student> set = new TreeSet<>(comparator);
        set.add(new Student("xiaobai",20,"男","123456"));
        set.add(new Student("xiaohong",21,"女","123456"));
        set.add(new Student("xiaoming",22,"男","123456"));
        for (Student s : set) {
            System.out.println(s);
        }
    }
    //可变参数必须放在最后面
    public static int sum(int...a){
        int total = 0;
        for (int i = 0; i < a.length; i++) {
            total += a[i];
        }
        return total;
    }
}
